<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package}.${bottomName}.${daoName}.${className}Mapper">

    <resultMap id="${className}Map" type="${package}.${bottomName}.dto.resp.${className}Resp">
        <#if columns?? &&columns?size gt 0>
            <#list columns as column>
                <#if column.dataType?? && column.dataType=='BLOB'>
                    <result column="${column.columnName}"  property="${column.attrname}" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/>
                <#else>
                    <result column="${column.columnName}"  property="${column.attrname}" />
                </#if>
            </#list>
        </#if>
    </resultMap>

    <sql id="allColumns">
        <#if columns?? &&columns?size gt 0>
            <#list columns as column>
                <#if column_index+1<columns?size>${column.columnName},<#else>${column.columnName}</#if>
            </#list>
        </#if>
    </sql>

    <sql id="dynamicWhere">
        <trim  suffixOverrides="," prefix="WHERE" prefixOverrides="AND">
            <#if columns?? &&columns?size gt 0>
                <#list columns as column>
                    <#if column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType=='String' && pk.columnName?? && pk.columnName == column.columnName>
                    <if test="req.${column.attrname}!=null and req.${column.attrname}!=''">AND ${column.columnName} = <#noparse>#{</#noparse>req.${column.attrname}<#noparse>}</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType=='String' && pk.columnName?? && pk.columnName!=column.columnName && dynamicLikeEnable?? && dynamicLikeEnable>
                    <if test="req.${column.attrname}!=null and req.${column.attrname}!=''">AND ${column.columnName} LIKE concat('%', <#noparse>#{</#noparse>req.${column.attrname}<#noparse>} ,'%')</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType=='String' && pk.columnName?? && pk.columnName!=column.columnName && dynamicLikeEnable?? && !dynamicLikeEnable>
                    <if test="req.${column.attrname}!=null and req.${column.attrname}!=''">AND ${column.columnName} = <#noparse>#{</#noparse>req.${column.attrname}<#noparse>}</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType!='String' && pk.columnName?? && pk.columnName == column.columnName>
                    <if test="req.${column.attrname}!=null ">AND ${column.columnName} = <#noparse>#{</#noparse>req.${column.attrname}<#noparse>}</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType!='String' && pk.columnName?? && pk.columnName != column.columnName>
                    <if test="req.${column.attrname}!=null ">AND ${column.columnName} = <#noparse>#{</#noparse>req.${column.attrname}<#noparse>}</if></#noparse>
                    </#if>
                </#list>
            </#if>
        </trim>
    </sql>

    <!-- page -->
    <select id="page" parameterType="${package}.${bottomName}.dto.req.${className}QueryReq"  resultMap="${className}Map">
        SELECT <include refid="allColumns" />
        FROM ${tableName}  <include refid="dynamicWhere" />
    </select>

    <!-- list -->
    <select id="list" parameterType="${package}.${bottomName}.dto.req.${className}QueryReq"  resultMap="${className}Map">
        SELECT <include refid="allColumns" />
        FROM ${tableName}  <include refid="dynamicWhere" />
    </select>

    <!-- view -->
    <select id="view"  resultMap="${className}Map">
        SELECT <include refid="allColumns" />
        FROM ${tableName}  WHERE ${pk.columnName} = <#noparse>#{</#noparse>${pk.attrname}<#noparse>}</#noparse> and logic_delete=false
    </select>

</mapper>